<?php
/**
 * Created by PhpStorm.
 * User: 69548
 * Date: 2018/7/24
 * Time: 10:32
 */

namespace app\api\controller;


use app\api\model\Ticket as TicketModel;
use think\Controller;

class Ticket extends Controller
{

    /**
     * 获取某一门票的套餐
     * @param $product_id 产品ID
     * @return array
     */
    public function getTicketSpecs($product_id,TicketModel $ticket)
    {
        //检查是否为门票产品
        $product = db('product p')->where(['use_type'=>3,'id'=>$product_id])->find();
        if(empty($product_id) || empty($product)) return ['code'=>4001,'门票产品不存在'];
        $package = $ticket->getTicketSpecs($product_id);

        if(array_key_exists('code',$package)) return $this->result([],$package['code'],$package['msg'],'json');
        return $this->result(['product'=>$product,'package'=>$package],200,'success','json');
    }

    /**
     * 获取门票的具体价格
     * @param $product_id 产品ID
     * @param $date 购买日期
     * @param $age 年龄
     * @return array
     */
    public function getTicketPriceByIDAndDate($product_id,$date,$age,TicketModel $ticket)
    {
        //检查是否为门票产品
        $product = db('product p')->where(['use_type' => 3, 'p.id' => $product_id])->find();
        if (empty($product_id) || empty($product)) return ['code' => 4001, '门票产品不存在'];
        $price = $ticket->getTicketPriceByIDAndDate($product_id, $date, $age);

        if (array_key_exists('code', $price)) return $this->result([], $price['code'], $price['msg'], 'json');
        return $this->result(['product' => $product, 'price' => $price], 200, 'success', 'json');
    }

}